document.addEventListener('DOMContentLoaded', function() {
    // 文件上传
    const uploadBtn = document.getElementById('upload-btn');
    const uploadModal = new bootstrap.Modal(document.getElementById('uploadModal'));

    uploadBtn.addEventListener('click', async function() {
        const fileInput = document.getElementById('file');
        const formData = new FormData();
        formData.append('file', fileInput.files[0]);

        try {
            const response = await fetch('/data/upload', {
                method: 'POST',
                body: formData
            });

            const result = await response.json();
            if (result.status === 'success') {
                alert('文件上传成功');
                location.reload();
            } else {
                alert('上传失败: ' + result.message);
            }
        } catch (error) {
            alert('上传失败: ' + error.message);
        }

        uploadModal.hide();
    });

    // 数据预览
    const previewBtns = document.querySelectorAll('.preview-btn');
    const previewModal = new bootstrap.Modal(document.getElementById('previewModal'));
    const previewTable = document.getElementById('preview-table');

    previewBtns.forEach(btn => {
        btn.addEventListener('click', async function() {
            const filename = this.dataset.filename;

            try {
                const response = await fetch(`/data/preview/${filename}`);
                const result = await response.json();

                if (result.status === 'success') {
                    // 设置表头
                    const thead = previewTable.querySelector('thead tr');
                    thead.innerHTML = result.columns.map(col => `<th>${col}</th>`).join('');

                    // 设置数据
                    const tbody = previewTable.querySelector('tbody');
                    tbody.innerHTML = result.data.map(row => `
                        <tr>${result.columns.map(col => `<td>${row[col]}</td>`).join('')}</tr>
                    `).join('');

                    previewModal.show();
                } else {
                    alert('预览失败: ' + result.message);
                }
            } catch (error) {
                alert('预览失败: ' + error.message);
            }
        });
    });

    // 删除数据集
    const deleteBtns = document.querySelectorAll('.delete-btn');

    deleteBtns.forEach(btn => {
        btn.addEventListener('click', async function() {
            const filename = this.dataset.filename;
            
            if (confirm(`确定要删除数据集 ${filename} 吗？`)) {
                try {
                    const response = await fetch(`/data/delete/${filename}`, {
                        method: 'POST'
                    });

                    const result = await response.json();
                    if (result.status === 'success') {
                        alert('删除成功');
                        location.reload();
                    } else {
                        alert('删除失败: ' + result.message);
                    }
                } catch (error) {
                    alert('删除失败: ' + error.message);
                }
            }
        });
    });
}); 